//引入Vue核心库
import Vue from 'vue'
//引入Vuex
import Vuex from 'vuex'
//应用Vuex插件
Vue.use(Vuex)

//准备actions对象——响应组件中用户的动作
const actions = {
  search(context, value) {
    console.log("这里是actions");
    context.commit('SEARCH', value)
  }
}
//准备mutations对象——修改state中的数据
const mutations = {
  SEARCH(context, value) {
    console.log("这里是mutations");
    console.log(value);
    // console.log(context);
    for (let i in context.words) {
      if (context.words[i].id == value) {
        console.log("succes");


        // localStorage.setItem('word', JSON.stringify(context.words[i].word))
        // localStorage.setItem('title', JSON.stringify(context.words[i].title))
        //该解决办法有点鸡肋，重新刷新页面就很呆
        // location.reload()
        context.word = context.words[i].word
        context.title = context.words[i].title
        // localStorage.setItem('word', this.$store.state.word)
        // localStorage.setItem('title', this.$store.state.title)
        context.status = 0
        return 0
      }

    }
    context.status = 1
    // alert("什么都没有哦~~~")
  }
}
//准备state对象——保存具体的数据
const state = {
  words: [{
      id: "xsq",
      word: "生日快乐啊啾啾",
      title: "YunMo"
    }, {
      id: "七月初七",
      word: "山水一程，三生有幸，七夕快乐!!!",
      title: "YunMo"
    },
    {
      id: "咕呱",
      word: "孤寡 孤寡孤寡孤寡 孤寡孤寡",
      title: "YunMo"
    }, {
      id: "云墨",
      word: "被发现了，溜了溜了",
      title: "YunMo"
    },
    {
      id: "单身狗",
      word: "你再骂！",
      title: "YunMo"
    },
    {
      id: "爱你",
      word: "爱你！！！",
      title: "YunMo"
    },
    {
      id: "我喜欢你",
      word: "我也是！！！",
      title: "YunMo"
    },
    {
      id: "今年七夕",
      word: "七夕？ 什么七夕？ 我只知道疯狂星期四！",
      title: "YunMo"
    },
    {
      id: "小黑子",
      word: "小黑子露出鸡脚了吧！",
      title: "YunMo"
    },


  ],
  word: localStorage.getItem('word'),
  title: localStorage.getItem('title'),
  status: '0',
  show: false
}

//创建并暴露store
export default new Vuex.Store({
  actions,
  mutations,
  state
})